####
#### Eroll Kuhn & Rahsaan Maxwell
#### Main Plot
### 10.2020


rm(list=ls())

library(tidyverse)
library(gridExtra)

# read in frame
df <- read.csv(".../6.Longer-Term Dynamics/Output/B.Tables/Clean_Combined.csv")


# pre-processing
df <- as_tibble(df)

library(expss)
df$facets <- ifelse(df$IV=="% Foreign-Born",1,
                    ifelse(df$IV=="% Non-EU",2,
                           ifelse(df$IV=="% Co-National",3,NA)))
df$facets <- factor(df$facets,
                       levels = c(1,2,3),
                       labels = c("% Foreign-Born", 
                                  "% Non-EU Foreign-Born",
                                  "% Co-National (Syrians Only)"))


df$year_jitter <- ifelse(df$Sample=="All",df$Year-0.1,
                         ifelse(df$Sample=="Stayers Only",df$Year+0.1,NA))

png(".../6.Longer-Term Dynamics/Output/D.Figures/CoefPlot_AcrossTime_V2.png",
    width=10,height=6,units='in',res=300)
ggplot(subset(df,df$Outcome=="Index")) +
  geom_pointrange(aes(x=year_jitter,y=Coef,ymin=Lower95,ymax=Upper95,shape=Sample),lwd=0.25) +
  geom_pointrange(aes(x=year_jitter,y=Coef,ymin=Lower90,ymax=Upper90,shape=Sample),lwd=0.5) +
  facet_wrap(~facets)+
  theme_bw() +
  geom_hline(yintercept = 0, lty = 2) +
  ylab("Effect on Feeling Welcome") +
  xlab("Year of Interview") +
  xlim(2015.5,2018.5) +
  theme(legend.direction = "horizontal",
        legend.position = "bottom",
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x = element_text(angle = 15, hjust = 1,size = 12),
        axis.text.y = element_text(size=12))
dev.off()  


############################################################################
# Across ObNr
rm(list=ls())

library(tidyverse)
library(gridExtra)

# read in frame
df <- read.csv(".../6.Longer-Term Dynamics/Output/B.Tables/Clean_ObNr.csv")


# pre-processing
df <- as_tibble(df)

library(expss)
df$facets <- ifelse(df$IV=="% Foreign-Born",1,
                    ifelse(df$IV=="% Non-EU",2,
                           ifelse(df$IV=="% Co-National",3,NA)))
df$facets <- factor(df$facets,
                    levels = c(1,2,3),
                    labels = c("% Foreign-Born", 
                               "% Non-EU Foreign-Born",
                               "% Co-National (Syrians Only)"))


df$ObNr_jitter <- ifelse(df$Sample=="All",df$`Observation.Number`-0.1,
                         ifelse(df$Sample=="Stayers Only",df$`Observation.Number`+0.1,NA))

png(".../6.Longer-Term Dynamics/Output/D.Figures/CoefPlot_AcrossObNr_V2.png",
    width=10,height=6,units='in',res=300)
ggplot(subset(df,df$Outcome=="Index")) +
  geom_pointrange(aes(x=ObNr_jitter,y=Coef,ymin=Lower95,ymax=Upper95,shape=Sample),lwd=0.25) +
  geom_pointrange(aes(x=ObNr_jitter,y=Coef,ymin=Lower90,ymax=Upper90,shape=Sample),lwd=0.5) +
  facet_wrap(~facets)+
  theme_bw() +
  geom_hline(yintercept = 0, lty = 2) +
  ylab("Effect on Feeling Welcome") +
  xlab("Individual Wave Number") +
  xlim(0.5,3.5) +
  theme(legend.direction = "horizontal",
        legend.position = "bottom",
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x = element_text(angle = 15, hjust = 1,size = 12),
        axis.text.y = element_text(size=12))
dev.off()  
